package uiLayer;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

import controllerLayer.CustomerCtr;
import controllerLayer.EmployeeCtr;
import controllerLayer.LoanCtr;
import controllerLayer.ProductCtr;
import controllerLayer.SaleCtr;

import modelLayer.Contractor;
import modelLayer.ContractorRecords;
import modelLayer.Customer;
import modelLayer.CustomerRecords;
import modelLayer.Employee;
import modelLayer.EmployeeRecords;
import modelLayer.LeaseProductsRecords;
import modelLayer.Loan;
import modelLayer.Product;
import modelLayer.ProductForLease;
import modelLayer.ProductRecords;
import modelLayer.SLIRecords;
import modelLayer.Sale;
import modelLayer.SaleLinesItem;

public class InputParser {
	private ReadFromFile rff;
	private ArrayList<String> data;
	private static InputParser instance;
	private static EmployeeCtr ectr;
	private static CustomerCtr cctr;
	private static ProductCtr pctr;
	private static LoanCtr lctr;
	private static SaleCtr sctr;

	public InputParser() {
	
		ReadFromFile rff = new ReadFromFile();
		data = rff.load();
		ectr = new EmployeeCtr();
		cctr = new CustomerCtr();
		pctr = new ProductCtr();
		lctr = new LoanCtr();
		sctr = new SaleCtr();
		
	}
	public static InputParser getInstance()       
	{
		if(instance == null)
		{
			instance = new InputParser();
		}
		return instance;
	}
	public void load(){
		for(String s: data){
			if(s!=null){
				Scanner scanner = new Scanner(s);
			scanner.useDelimiter(",");
			int type = scanner.nextInt();
				switch(type){
					case 1:parseCustomer(s);
					break;
					case 2:parseEmployee(s);
					break;
					case 3:parseLoan(s);
					break;
					case 4:parseSale(s);
					break;
					case 5:parseContractor(s);
					break;
					case 6:parseProduct(s);
					break;
					case 7:parseSLI(s);
					break;
					case 86:parsePFL(s);
					}
				}
			}
		}
	private void parsePFL(String s) {
		Scanner scanner = new Scanner(s);
		scanner.useDelimiter(",");
		if(scanner.nextInt()==86){
			int Id = parseInt(scanner.next());
			String name = scanner.next();
			String description = scanner.next();
			double price = scanner.nextDouble();
			String location = scanner.next();
			String category = scanner.next();
			double quantity = parseDouble(scanner.next());
			double alert = parseDouble(scanner.next());
			String measure = scanner.next();
			String state = scanner.next();
			if(quantity==0)state = "loaned";
			else state = "available";
			ProductForLease p1 = new ProductForLease(Id, name, description, price, quantity, category, location, state);
			LeaseProductsRecords.getInstance().getItemsForLoan().add(p1);
		}
		
	}
	private void parseSLI(String s) {
		Scanner scanner = new Scanner(s);
		scanner.useDelimiter(",");
		if(scanner.nextInt()==7){
		String name = scanner.next();
		double price = scanner.nextDouble();
		double quantity = scanner.nextDouble();
		String measure = scanner.next();
		String id = scanner.next();
		if(!id.startsWith("c")){
			Product product = pctr.getItem(id);
		
		product.setQuantity(quantity);
		product.setName(name);
		product.setPrice(price);
		product.setMeasure(measure);
		}
		if(id.startsWith("c")){
			SaleLinesItem sli = new SaleLinesItem(id,name);
			SLIRecords.getInstance().getSlis().add(sli);
		}
		}
	}
	private void parseProduct(String s) {
		Scanner scanner = new Scanner(s);
		scanner.useDelimiter(",");
		if(scanner.nextInt()==6){
			int Id = parseInt(scanner.next());
			String name = scanner.next();
			String description = scanner.next();
			double price = scanner.nextDouble();
			String location = scanner.next();
			String category = scanner.next();
			double quantity = parseDouble(scanner.next());
			double alert = parseDouble(scanner.next());
			String measure = scanner.next();
			
			Product product = new Product(Id,name,description,price,quantity,category,location,alert,measure);
			SaleLinesItem sli = new SaleLinesItem(product);
			SLIRecords.getInstance().getSlis().add(sli);
			ProductRecords.getInstance().getProducts().add(product);
		}
		
	}
	private void parseContractor(String s) {
		Scanner scanner = new Scanner(s);
		scanner.useDelimiter(",");
		if(scanner.nextInt()==5){
			String Id = scanner.next();
			String name = scanner.next();
			String address = scanner.next();
			String phoneNr = scanner.next();
			double moneyPaid = parseDouble(scanner.next());
			ArrayList<Product> products = new ArrayList<Product>();
			while(scanner.hasNext()){
				products.add(pctr.getItem(scanner.next()));
			}
			Contractor c1 = new Contractor(Id,name,address,phoneNr);
			c1.setProducts(products);
			c1.setMoneyPaid(moneyPaid);
			ContractorRecords.getInstance().getContractors().add(c1);
		}
		
	}
	private void parseSale(String s) {
		ArrayList<SaleLinesItem> sls = new ArrayList<SaleLinesItem>();
		Scanner scanner = new Scanner(s);
		scanner.useDelimiter(",");
		if(scanner.nextInt()==4){
			int Id = parseInt(scanner.next());
			String cId = scanner.next();
			String eId = scanner.next();
			while(scanner.hasNext()){
				sls.add(pctr.getSaleLinesItem(scanner.next()));
			}
			Customer c1 = cctr.getCustomer(cId);
			Employee e1 = ectr.getEmployee(eId);
			Sale sale = new Sale(Id,c1,e1,sls); 
			sctr.getOffers().add(sale);
		}
		
	}
	private void parseLoan(String s) {
		
		Scanner scanner = new Scanner(s);
		scanner.useDelimiter(",");
		if(scanner.nextInt()==3){
			String borrowDate = scanner.next();
			int period = parseInt(scanner.next());
			String CId = scanner.next();
			String EId = scanner.next();
			int Id = parseInt(scanner.next());
			Customer c1 = cctr.getCustomer(CId);
			Employee e1 = ectr.getEmployee(EId);
			ProductForLease p1 = lctr.getProduct(Id);
			Loan l1 = new Loan(borrowDate,period,c1,p1,e1);
			lctr.getLoans().add(l1);
		}
		
	}
	private void parseEmployee(String s) {
		ArrayList<SaleLinesItem> emp = new ArrayList<SaleLinesItem>();
		Scanner scanner = new Scanner(s);
		scanner.useDelimiter(",");
		if(scanner.nextInt()==2){
			String Id = scanner.next();
			String name = scanner.next();
			String address = scanner.next();
			String phoneNumber = scanner.next();
			double moneyEarned = parseDouble(scanner.next());
			double salary = parseDouble(scanner.next());
			while(scanner.hasNext()){
				emp.add(pctr.getSaleLinesItem(scanner.next()));
			}
			Employee e1 = new Employee(Id, name, address, phoneNumber, salary, moneyEarned);
			e1.setItemsSold(emp);
			EmployeeRecords.getInstance().getEmployees().add(e1);
			}
	
	}
	private void parseCustomer(String s) {
		ArrayList<SaleLinesItem> cust = new ArrayList<SaleLinesItem>();
		Scanner scanner = new Scanner(s);
		scanner.useDelimiter(",");
		if(scanner.nextInt()==1){
			String Id = scanner.next();
			String name = scanner.next();
			String address = scanner.next();
			String phoneNumber = scanner.next();
			double moneySpent = parseDouble(scanner.next());
			double discount = parseDouble(scanner.next());
			while(scanner.hasNext()){
				cust.add(pctr.getSaleLinesItem(scanner.next()));
			}
			Customer c1 = new Customer(Id,name,address,phoneNumber,moneySpent,discount);
			c1.setItemsBought(cust);
			CustomerRecords.getInstance().getCustomers().add(c1);
		}
		
	}
	public int parseInt(String s){
		Scanner scanner = new Scanner(s);
		try{
			int i = scanner.nextInt();
			return i;
		}
		catch(NumberFormatException Nfe){
			System.out.println("you must enter an integer value: entered 0");
			return 0;
		}
		catch(InputMismatchException ime){
		System.out.println("you must enter an integer value: entered 0");
		return 0;
	}
		catch(NullPointerException npe){
			System.out.println("You must create at first.");
			return -1;
		}
		catch(NoSuchElementException nsee){
			System.out.println("entered 0");
			return 0;
		}
	}
	public double parseDouble(String s){
		Scanner scanner = new Scanner(s);
		try{
			double d = scanner.nextDouble();
			return d;
		}
		catch(NumberFormatException nfe){
			System.out.println("you must enter a double value");
			return 0;
		}
		catch(InputMismatchException ime){
			System.out.println("you must enter a double value");
			return 0;
		}
		catch(NoSuchElementException nsee){
			System.out.println("entered 0");
			return 0;
		}
	}
	public String parseString(String s){
		Scanner scanner = new Scanner(s);
		try{
			return scanner.next();
		}
		catch(NullPointerException npe){
			return "";
		}
	}
}
